window.debug = true;
window.PAGECONFIG = {
    curPageName:'topic'
};
var timer_count = null;
define(['utils', 'swiper', 'touch', 'swiperHashnav'], function (utils, swiper, touch, swiperHashnav) {
    var global = window.$CONFIG;
    var WallId = window.$CONFIG['wallid'];
    var bigScreenFrame = {
        init: function (options) {
            var that = this;
            that.opt = $.extend(true, {}, options);

            window.tap = 'click';
            that.initMenuData();
            // that.bindEvent();//游戏人数统计
            that.getTmp(8);

            that.defautlSetting();
        },
        defautlSetting:function(){
            var body = $('body');
            var style = '<style>';

            //主题切换
            var themeFile = '';

             // W_SKIN.TemplateName = 'ch-married';

            if(W_SKIN){
                W_SKIN.TemplateName = W_SKIN.TemplateName || 'white';

                themeFile = $('<link  type="text/css" rel="stylesheet"  href="../addons/haoman_dpm/newhb_img/swallv5/css/theme/' + W_SKIN.TemplateName + '.css"/>');
                $('body').append(themeFile);
                $('body').addClass(W_SKIN.TemplateName);
            }

            //设置背景
            if(W_SKIN &&W_SKIN.BackgroundImage&& !W_SKIN.BackgroundVodie){
                style += '.body-skin{background-image:' + 'url("' + W_SKIN.BackgroundImage +'")' + '}';
            }
            if(W_SKIN && W_SKIN.ContentBgColorRGB){
                style += '.' + W_SKIN.TemplateName + ' .container .content{background-color:' + '' + W_SKIN.ContentBgColorRGB + ' !important}';
                style += '.' + W_SKIN.TemplateName + ' .content-vote .vote-canvas{background-color:rgba(0,0,0,0) !important}';
            }

            // //定制样式
            // if(W_SKIN
            //     &&  W_SKIN.CustomCss){
            //     style += W_SKIN.CustomCss;
            // }

            style += '</style>';
            body.append(style);
            

        },
        menuData: [],
        initMenuData: function () {
            var that = this;

            var menuData = $('#MenuList').data('funmodule');
            // var menus = menuData.split(',');
            var usedModuleList = ["topic", "lottery", "crazy", "vote", "picture", "guess", "team","mark","bonus"];
            for (var i = 0, len = usedModuleList.length; i < len; i++) {
                var type = usedModuleList[i];
                // if (menuData.indexOf(type) == -1) continue;
                var tmpUrl = '';

                //if (type === 'shang') continue;
                //if (type === 'wish') continue;
                tmpUrl = CONFIG.URL.StaticAll + 'html/' + type + '.html';
                that.menuData.push({
                    index: i,
                    typeName: type,
                    pageName: type,
                    tmpUrl: tmpUrl
                });
            }
            //for(var i=0,len=menus.length;i<len;i++){
            //  var type = menus[i];
            //  var tmpUrl = '';

            //  if (type === 'shang') continue;
            //    tmpUrl = CONFIG.URL.StaticAll + 'html/' + type + '.html';
            //    that.menuData.push({
            //        index: i,
            //        typeName: type,
            //        pageName: type,
            //        tmpUrl: tmpUrl
            //    });
            //}
            that.menuData.push({
                index: that.menuData.length,
                typeName: 'full',
                tmpUrl: ''
            });
        },
        initSwiper: function () {
            var that = this;
            window.myFrameSwiper = new Swiper('.swiper-container', {
                pagination: '.pagination',
                paginationClickable: true,
                touchRatio: 0.6,
                speed: 500,
                hashNav: true,
                onFirstInit: function (swiper) {
                    //当面板第一次初始化完成后执行的函数
                    var activeIndex = swiper.activeIndex;
                    //设置当前的page
                    global['act'] = that.menuData[activeIndex].pageName;
                    window.PAGECONFIG.curPageName = global['act'];

                },
                onSlideChangeStart: function (swiper) {

                    // 当面板切换过来开始时的回调函数
                    var activeIndex = swiper.activeIndex;
                    // 设置当前的page
                    global['act'] = that.menuData[activeIndex].pageName;
                    window.PAGECONFIG.curPageName = global['act'];

                    that.getTmp(activeIndex);

                    // 菜单切换
                    $('#MenuBox .item').eq(activeIndex).addClass('active').siblings().removeClass('active');
                },
                onSlideChangeEnd: function (swiper) {
                    // 当面板切换过来结束时的回调函数
                    var idx = swiper.activeIndex;
                    var menu_num = $('#MenuList .item').length-1;
                    for (var i = 0; i < menu_num; i++) {
                        var module = that[that.menuData[i].pageName];
                        if (module && module.hasOwnProperty('moduleFlag')) {
                            module.moduleFlag = true;
                            clearInterval(module.moduleTimer);
                        }
                    }
                    var pageName = that.menuData[idx].pageName;
                    if (pageName === 'topic' || pageName === 'picture') {
                        var module = that[pageName];
                        if (module) {
                            module.moduleFlag = false;
                            clearInterval(module.moduleTimer);
                            module.pollingLatestData();
                        }
                    }
                },
                onSlidePrev: function (swiper) {
                    // 当切换到上一个面板时的回调函数
                },
                onSlideNext: function (swiper) {
                    // 当切换到下一个面板时的回调函数
                },
                onTouchStart: function (swiper) {
                    // 当触控开始时触发的函数
                },
                onTouchEnd: function (swiper) {
                    // 当触控结束时触发的函数
                }
            });

            that.getTmp(0);
        },
        bindEvent: function () {
            var that = this;
            that.getJoinNum();
        },
        getTmp: function (index) {
            var that = this;
            // console.log(67);
            // console.log(that.menuData);
                utils.ajax({
                    url: that.menuData[index].tmpUrl,
                    type: 'get',
                    data: {
                        _fromappdata: $CONFIG._fromappdata
                    },
                    dataType: 'html',
                    // context: $('.swiper-slide-parent').eq(index), // 当前上下文，防止多次提交
                    success: function (data) {
                        if (data) {

                            var currentSlide = $('.dpm_hb');

                            currentSlide.html(data);

                             // 边框8边模式
                            var dirs = ['top','right-top','right','right-bottom','bottom','left-bottom','left','left-top'];
                            var html ='<div class="frame-blocks">';

                            for(var i = 0 ; i < dirs.length ; i ++){
                                html += '<div class="block ' + dirs[i] + '"></div>';
                            }

                            html +='</div>';

                            currentSlide.find('.frame-wrapper').append(html);


                            var typeName = that.menuData[index].typeName;
                            if (typeName.toLowerCase() === 'logowall')
                                return false;

                            // 异步加载对应模块JS
                            var eleRandomId = (new Date()).getTime() + parseInt(Math.random() * 100000);

                            require([CONFIG.URL.StaticJS + 'app/big_screen_' + typeName + '.js?v=' + eleRandomId], function (module) {

                                $('.img-wall-qrcode').attr('src', W_SKIN.Logo);
                                if (W_SKIN.Banner) {
                                    $('.container > .header .img-logo').remove();
                                    $('.wall-title').addClass('has-banner').html('<img src="' + W_SKIN.Banner + '">');
                                } else {
                                    $('.container > .header .img-logo').attr('src', W_SKIN.Logo);
                                    $('.wall-title > h3').text(W_Data.RoomName);
                                    if (W_Data.SubTitle) {
                                        $('.wall-title > h4').text(W_Data.SubTitle);
                                    }
                                }
                                that[that.menuData[index].pageName] = module;
                                that[that.menuData[index].pageName].init(that);
                            });
                        }
                    }
                });

        },
        getJoinNum: function () {
            if (timer_count == null)timer_count = this;
                utils.ajax({
                    url: Get_RoomUserCount,
                    data: {
                        WallId: WallId
                    },
                    success: function (json) {
                        if (json.successful && json.code > 0) {
                            $('#bonusWaitNum').text(json.data);
                        }
                    },
                    complete: function(o) {
                        if ($(".qr-code-box").is(":hidden") == false) {
                            window.setTimeout(timer_count.getJoinNum, 3000);
                        }
                    }
                });

        },
        scroll: function () {
            $('body').on('mouseenter mouseleave', '.is-scroll', function (e) {
                $(this).toggleClass('is-scroll-hover', e.type === 'mouseenter');
            });
        },
        gotoFullscreen: function (element, method) {
            var el = document.documentElement,
                rfs = el.requestFullScreen 
                    || el.webkitRequestFullScreen
                    || el.mozRequestFullScreen
                    || el.msRequestFullScreen;

            if (typeof rfs !== 'undefined' && rfs) {
                rfs.call(el);
            } else if (typeof window.ActiveXObject !== 'undefined') {
                var wscript = new ActiveXObject('WScript.Shell');
                if (wscript) {
                    wscript.SendKeys('{F11}');
                }
            }
        },
        exitFullscreen: function () {
            var de = document;
            if (de.exitFullscreen) {
                de.exitFullscreen();
            } else if (de.mozCancelFullScreen) {
                de.mozCancelFullScreen();
            } else if (de.webkitCancelFullScreen) {
                de.webkitCancelFullScreen();
            }
        }
    };

    return bigScreenFrame;
});